package com.cy.java;

import com.sun.media.sound.SoftTuning;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) throws Exception{
        Class test = Test01.class;

        method1(test);

        Test01 test01 = method2(test);
        System.out.println(test01.getAge()+test01.getName());
        Test01 test011 = method3(test);
        System.out.println(test011.getAge());
    }
    public static void method1(Class test) throws Exception{
        Field age = test.getDeclaredField("age");
        age.setAccessible(true);
        Object o = test.newInstance();

        age.set(o,10);

        System.out.println(age.get(o));

    }
    public static Test01 method2(Class test) throws Exception {
        Constructor c  = test.getDeclaredConstructor(int.class,String.class);
        Object hhh = c.newInstance(20, "hhh");

        return (Test01) hhh;

    }
    public static Test01 method3(Class test) throws Exception {
        Method setAge = test.getDeclaredMethod("setAge", int.class);
        Object o = test.newInstance();
        setAge.invoke(o, 30);

        return (Test01)o;
    }
}
class Test01{
    private int age;
    private String name;
    public Test01(){}
    public Test01(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}